package com.ssy.lingxi.product.entity.commodity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.ssy.lingxi.product.entity.Goods;
import com.ssy.lingxi.product.handler.converter.JpaJsonToMapConverter;
import com.ssy.lingxi.product.handler.converter.JpaStringToArrayConverter;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 商品实体类
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/28
 */
@Getter
@Setter
@Entity
@Table(schema = "public", name = "product_commodity_unitPriceAndPic", indexes = {@Index(name = "product_commodity_unit_price_and_pic_is_more_price_idx", columnList = "isMorePrice"),
        @Index(name = "product_commodity_unit_price_and_pic_upper_commodity_sku_id_idx", columnList = "upperCommoditySkuId")})
public class CommodityUnitPriceAndPic implements Serializable {
    private static final long serialVersionUID = 2338803439820663815L;
    /**
     * 主键id
     */
    @Id
    @GeneratedValue(strategy= GenerationType.SEQUENCE, generator="commodity_unitPriceAndPic_seq")
    @SequenceGenerator(sequenceName="product_commodity_unitPriceAndPic_seq", name="commodity_unitPriceAndPic_seq",allocationSize = 1)
    private Long id;

    /**
     * 商品id
     */
    @JsonIgnore
    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name="commodityId",referencedColumnName = "id")
    private Commodity commodity;

    /**
     * 货品
     */
    @OneToOne(cascade = {CascadeType.PERSIST})
    @JoinColumn(name="goodsId",referencedColumnName = "id")
    private Goods goods;

    /**
     * 商品属性
     */
    @JsonManagedReference
    @OneToMany(mappedBy = "commodityUnitPriceAndPic", cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
    private List<CommodityGoodsAttribute> attributeAndValueList;

    /**
     * 是否阶梯价
     */
    @Column(columnDefinition = "boolean")
    private Boolean isMorePrice;

    /**
     * 商品单价
     */
    @Convert(converter = JpaJsonToMapConverter.class)
    @Column(columnDefinition="jsonb", nullable = false)
    private Map<String,Double> unitPrice;

    /**
     * 副单位单价换算比率
     */
    @Column(columnDefinition = "decimal")
    private Double priceRate = 1d;

    /**
     * 商品图片
     */
    @Convert(converter = JpaStringToArrayConverter.class)
    @Column(columnDefinition = "text", nullable = false)
    private String[] commodityPic;

    /**
     * 上游商品skuId
     */
    @Column(columnDefinition = "int8")
    private Long upperCommoditySkuId;

    /**
     * HS编码
     */
    @Column(columnDefinition = "varchar(50)")
    private String hsCode;
}
